package com.jilian.mydemo.modul.my.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jilian.mydemo.R;
import com.jilian.mydemo.common.dto.VideoArticlesDto;
import com.jilian.mydemo.listener.CustomItemClickListener;

import java.util.List;

/**
 * 我的收藏
 * 列表适配器
 */
public class MyCollectionAdapter extends RecyclerView.Adapter<MyCollectionAdapter.ViewHolder> {
    private Activity mContext;
    private List<VideoArticlesDto> datas;
    private CustomItemClickListener listener;

    public MyCollectionAdapter(Activity context, List<VideoArticlesDto> datas, CustomItemClickListener listener) {
        mContext = context;
        this.datas = datas;
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_mycollection, parent, false);
        ViewHolder viewHolder = new ViewHolder(view, listener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        //1文章，2视频
        if(datas.get(position).getType()==1){
            //显示图片
            Glide
                    .with(mContext)
                    .load(datas.get(position).getPath())
                    .into(holder.ivHead);
        }
        else{
            //显示视频
            holder.ivHead.setImageBitmap(datas.get(position).getBitmap());

        }
        holder.tvTitle.setText(datas.get(position).getTitle());
        holder.tvContent.setText(datas.get(position).getContent());

    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivHead;
        private TextView tvTitle;
        private TextView tvContent;


        public ViewHolder(final View itemView, final CustomItemClickListener listener) {
            super(itemView);

            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            tvTitle = (TextView)itemView. findViewById(R.id.tv_title);
            tvContent = (TextView)itemView. findViewById(R.id.tv_content);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick(itemView,getAdapterPosition());
                }
            });
        }

    }
}
